package org.nggen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.beetl.core.BeetlKit;
import org.nggen.config.GenConfig;
import org.nggen.config.IOKit;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;

public class Generator {

	private static Logger logger = Logger.getLogger(Generator.class);

	private GenConfig config;

	public Generator(GenConfig config) {
		this.config = config;
	}

	public void gen() {
		File fileDir = new File(config.getBasePath());
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("modulename", config.getModuleName());

		String firstUpModulename = config.getModuleName().substring(0, 1).toUpperCase() + config.getModuleName().substring(1);
		
		createFile(config.getModuleViewPath(), config.getModuleName() + ".html", "module.html.btl", map);
		String controllerName = firstUpModulename + "Controller.js";
		createFile(config.getControllerPath(), controllerName, "ModuleController.js.btl", map);
		if (config.isHasService()) {
			String serviceName = firstUpModulename + "Service.js";
			createFile(config.getServicePath(), serviceName, "ModuleService.js.btl", map);
		}
		
		if(config.isHasCss()){
			createFile(config.getModuleCssPath(), config.getModuleName() + ".css", "module.css.btl", map);
		}
		
		if(config.getDirectiveName() != null){
			for (int i = 0; i < config.getDirectiveName().length; i++) {
				String directive = config.getDirectiveName()[i];
				if(StrKit.isBlank(directive)){
					continue;
				}
				String viewPath = config.getDirectiveViewPath()[i];
				String jsPath = config.getDirectiveJsPath()[i];
				
				String name = directive;
				int index = name.indexOf("-");
				while(index != -1){
					name = name.substring(0, index) + name.substring(index + 1, index + 2).toUpperCase() + name.substring(index + 2); 
					index = name.indexOf("-");
				}
				map.put("directivetag", directive);
				map.put("directiveName", name);
				
				createFile(viewPath, directive + ".html", "directive.html.btl", map);
				createFile(jsPath, directive + ".js", "directive.js.btl", map);
			}
		}
		
	}

	private void createFile(String path, String filename, String template, Map<String, Object> map) {
		String content = IOKit.readToString(new File(PathKit.getRootClassPath() + File.separator + template));
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		String fileName = path + File.separator + filename;

		try {
			BeetlKit.renderTo(content, new FileWriter(new File(fileName)), map);
		} catch (IOException e) {
			logger.error("gen " + filename + " error!", e);
		}

	}

	public static void main(String[] args) {

		GenConfig config = new GenConfig("init.properties");
		Generator gen = new Generator(config);
		gen.gen();

	}

}
